package com.tyc.es.service;

import com.tyc.es.model.ESBook;
import com.tyc.es.repository.ESBookRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;

/**
 * @author geng
 * 2020/12/19
 */
@Slf4j
@Service
public class BookService {
    @Autowired
    private  ESBookRepository esBookRepository;


    public String addBook(ESBook book) {
        book.setId(UUID.randomUUID().toString());
        try {
            esBookRepository.save(book);
            return book.getId();
        } catch (Exception e) {
            log.error(String.format("保存ES错误！%s", e.getMessage()));
        }
        return null;
    }

    public List<ESBook> search(String keyword) {
        return esBookRepository.findByTitleOrAuthor(keyword, keyword);
    }

    public SearchHits<ESBook> searchBookTitle(String keyword) {
        return esBookRepository.find(keyword);
    }
}
